﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using corpus_platform.Data;

namespace corpus_platform.ViewModels
{
    public class MetaInformatioinConfirmViewModel : BindableBase, IDialogAware
    {
        private string corpus = "";
        public string Corpus
        {
            get { return corpus; }
            set { SetProperty(ref corpus, value); }
        }

        private string source = "";
        public string Source
        {
            get { return source; }
            set { SetProperty(ref source, value); }
        }

        private string corpusId = "";
        public string CorpusId
        {
            get { return corpusId; }
            set { SetProperty(ref corpusId, value); }
        }

        public string Title => "元信息确认";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            this.Corpus = parameters.GetValue<string>("corpus");
        }

        public void RaiseRequestClose(IDialogResult result)
        {
            RequestClose?.Invoke(result);
        }

        private DelegateCommand saveCommand;
        public DelegateCommand SaveCommand =>
            saveCommand ?? (saveCommand = new DelegateCommand(ExecuteSaveCommand));

        void ExecuteSaveCommand()
        {
            MetaInformation metaInformation = new()
            {
                Source = this.Source,
                Corpus = this.Corpus,
                CorpusId = this.CorpusId,
            };
            DialogParameters dialogParameters = new DialogParameters
            {
                { "metaInformation", metaInformation }
            };
            RaiseRequestClose(new DialogResult(ButtonResult.OK, dialogParameters));
        }

        private DelegateCommand cancelCommand;
        public DelegateCommand CancelCommand =>
            cancelCommand ?? (cancelCommand = new DelegateCommand(ExecuteCancelCommand));

        void ExecuteCancelCommand()
        {
            RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
        }
    }
}
